<?php

/**
 * 加密算法
 * @param $string 密码字符串
 * @param string $operation EN 加密 DECODE 解密
 * @param string $key
 * @param int $expiry
 * @return bool|string
 */
function CODE($string, $operation = 'DECODE', $key = 'AX', $expiry = 0)
{
    // 动态密匙长度，相同的明文会生成不同密文就是依靠动态密匙
    $ckey_length = 4;

    // 密匙
    $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);

    // 密匙a会参与加解密
    $keya = md5(substr($key, 0, 16));
    // 密匙b会用来做数据完整性验证
    $keyb = md5(substr($key, 16, 16));
    // 密匙c用于变化生成的密文
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) :
        substr(md5(microtime()), -$ckey_length)) : '';
    // 参与运算的密匙
    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);
    // 明文，前10位用来保存时间戳，解密时验证数据有效性，10到26位用来保存$keyb(密匙b)，
//解密时会通过这个密匙验证数据完整性
    // 如果是解码的话，会从第$ckey_length位开始，因为密文前$ckey_length位保存 动态密匙，以保证解密正确
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) :
        sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    // 产生密匙簿
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    // 用固定的算法，打乱密匙簿，增加随机性，好像很复杂，实际上对并不会增加密文的强度
    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    // 核心加解密部分
    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        // 从密匙簿得出密匙进行异或，再转成字符
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if ($operation == 'DECODE') {
        // 验证数据有效性，请看未加密明文的格式
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&
            substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        // 把动态密匙保存在密文里，这也是为什么同样的明文，生产不同密文后能解密的原因
        // 因为加密后的密文可能是一些特殊字符，复制过程可能会丢失，所以用base64编码
        return $keyc . str_replace('=', '', base64_encode($result));
    }
}



function MAkE_COUPON() {
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $rand = $code[rand(0,25)]
        .strtoupper(dechex(date('m')))
        .date('d').substr(time(),-5)
        .substr(microtime(),2,5)
        .sprintf('%02d',rand(0,99));
    for(
        $a = md5( $rand, true ),
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
        $d = '',
        $f = 0;
        $f < 8;
        $g = ord( $a[ $f ] ),
        $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
        $f++
    );
    return $d;
}



/**
 * 生成订单
 * @return string
 */
function ORDER_SN()
{
    $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','M','L','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    return $orderSn;
}

/**
 * 隐藏手机号码
 * @param $phone
 * @return null|string|string[]
 */
function MOBILE_PHONE_HIDDEN($phone)
{
    $is_what = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i', $phone); //固定电话
    if ($is_what == 1)
        return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i', '$1****$2', $phone);
    else
        return preg_replace('/(1[35789]{1}[0-9])[0-9]{4}([0-9]{4})/i', '$1****$2', $phone);
}

/**
 * 判断手机号码是否合法
 * @param $mobile 手机号码
 * @return bool
 */
function CHECK_MOBILE($mobile)
{
    $pattern = '/^(0|86|17951)?(888|13[0-9]|15[012356789]|17[0-9]|111|18[0-9]|14[57])[0-9]{8}$/';
    if (preg_match($pattern, $mobile)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

/**
 * 判断用户名是否合法
 * @param $name 小写字母开头，只能包含小写字母3-16位
 * @return bool
 */
function CHECK_USERNAME($name)
{
    $patten = '/^[a-z]{3,12}$/';
    if (preg_match($patten, $name)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

/**
 * httpPost请求
 * @param $url
 * @param $data
 * @return mixed
 */
function POST_JSON_RESPONSE($url, $data)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json; charset=utf-8',
        'Content-Length: ' . strlen($data)
    ]);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $response;
}

/**
 * 加盐加密
 * @param string $password 密码
 * @param $salt 加密字符串
 * @return string
 */
function ENCRYPTION($password = '123456', $salt)
{
    if (empty($salt)) {
        return false;
    }
    $password = md5($password);
    return hash("sha256", $password . $salt);
}

/**
 * 生成随机字符串
 * @param int $length 字符串长度
 * @return bool|string
 */
function RANDOM($length = 6)
{
    $intermediateSalt = md5(uniqid(rand(), true));
    return substr($intermediateSalt, 0, $length);
}

/**
 * 提现银行
 * @param int $type
 * @return bool|string
 */
function BANK($type = 1)
{
    $bank = '';
    if ($type == 1) {
        $bank = '中国银行';
    } elseif ($type == 2) {
        $bank = '中国工商银行';
    } elseif ($type == 3) {
        $bank = '中国农业银行';
    } elseif ($type == 4) {
        $bank = '中国建设银行';
    }
    if (empty($bank)) {
        return false;
    } else {
        return $bank;
    }
}

/**
 * 保留两位小数
 * @param null $money
 * @return float|int
 */
function RETAIN_TWO($money = null)
{
    return intval($money * 100) / 100;
}


/**
 * 生成唯一门店报单编号
 * @param null $money
 * @return float|int
 */
function STORES_NUMBER( $number = '' , $stores_id = '0' )
{
    //$DateChange = substr( date( 'Ymd', time() ), 2 );
    $StoresLength= strlen( $stores_id );
    switch ( $StoresLength ) {
        case '1':
            $code = '000'.$stores_id;
            break;
       case '2':
            $code = '00'.$stores_id;
            break;
        case '3':
            $code = '0'.$stores_id;
            break;
        case '4':
            $code = $stores_id;
            break;
    }
    //return $number.$code.$DateChange;
    return $number.$code;
}


/**
 * 金额计算
 * @param int $credit1 电子币
 * @param int $credit2 奖励币
 * @param int $credit4 积分
 * @param int $rulingprice 商品价格
 * @return array
 */
function FIGURE($credit1 = 0, $credit2 = 0, $rulingprice = 0)
{
//    //只能使用的积分支付
//    $integral = intval($rulingprice / 2);
//    if ($integral > $credit4) {
//        $totalMoney = $credit1 + $credit2 + $credit4 + $voucher;
//    } else {
//        $totalMoney = $credit1 + $credit2 + $integral + $voucher;
//    }
    $money['credit1'] = 0; //电子币 默认电子币先用
    $money['credit2'] = 0;// 奖励币
    if ($credit2 < 0){
        $totalMoney = $credit1;
    } else {
        $totalMoney = $credit1 + $credit2 ;
    }
    if ($totalMoney < $rulingprice) {
        return ['code' => 0, 'data' => '余额不足'];
    }
    //使用电子币
    $money['credit1'] = ($credit1 < $rulingprice) ? $credit1 : $rulingprice;
    //使用奖励币
    $need = $rulingprice - $credit1; //除去代金券还需支付
    if ($need > 0) {
        $money['credit2'] = ($credit2 >= $need) ? $need : 0;
    }

    if (empty($money)) {
        return ['code' => 0, 'data' => '没有数据'];
    } else {
        return ['code' => 1, 'data' => $money];
    }
}

/**
 * 报单使用阅读积分
 * @param int $credit1
 * @param int $credit2
 * @param int $credit4
 * @param $deduction
 * @param $rulingprice
 * @return array
 */
function FIGURE_READ($credit1 = 0, $credit2 = 0 ,$credit4 = 0 ,$deduction ,$rulingprice = 0 )
{
    //使用的积分抵扣
    $integral = intval($rulingprice * $deduction/100);
//    var_dump($integral);die;
    if ($integral > $credit4) {
        $totalMoney = $credit1 + $credit2 + $credit4;
    } else {
        $totalMoney = $credit1 + $credit2 + $integral;
    }
    if ($totalMoney < $rulingprice){
        return ['code' => 0, 'data' => '余额不足'];
    }
    $money['credit4'] = 0;//积分优先抵扣
    $money['credit1'] = 0; //电子币 默认电子币先用
    $money['credit2'] = 0;// 奖励币

    //使用积分
    $money['credit4'] = ($credit4 < $integral) ? (float)$credit4 : $integral;
    //使用电子币
    $needCredit1 = $rulingprice - $money['credit4']; //除去积分还需支付
    if ($needCredit1 > 0) {
        $money['credit1'] = ($credit1 >= $needCredit1) ? (float)$needCredit1 : $credit1;
        $needCredit2 = $needCredit1 - $money['credit1'];
        if ($needCredit2 > 0){
            $money['credit2'] = $needCredit2;
        }
    }

    if (empty($money)) {
        return ['code' => 0, 'data' => '没有数据'];
    } else {
        return ['code' => 1, 'data' => $money];
    }
}


/**
 * 代金券
 * 电子币
 * 积分
 * 总价格
 */
function FIGUR($voucher,$credit1 = 0, $credit2 = 0, $credit4 = 0, $rulingprice = 0)
{
    //只能使用的积分支付
//    $integral = intval($rulingprice / 2);
//    if ($integral > $credit4) {
//        $totalMoney =$voucher + $credit1 + $credit2 + $credit4;
//    } else {
//        $totalMoney =$voucher + $credit1 + $credit2 + $integral;
//    }
//    $rulingprice = 255;
    $totalMoney =$voucher + $credit1 + $credit2 + $credit4;
    $money['voucher'] = 0;
    $money['credit1'] = 0; //电子币 默认电子币先用
    $money['credit2'] = 0;// 奖励币
    $money['credit4'] = 0;// 积分
    if ($totalMoney < $rulingprice) {
        return ['code' => 0, 'data' => '余额不足'];
    }
    //使用代金劵
    $money['voucher'] = ($voucher < $rulingprice) ? $voucher : $rulingprice;
    //使用电子币
    $needCredit1 = bcsub($rulingprice,$voucher,2); //除去代金劵还需支付
    if ($needCredit1 > 0) {
        //电子币支付
        $money['credit1'] = ($needCredit1 < $credit1) ? $needCredit1 : $credit1;
        $needCredit2= bcsub($needCredit1,$money['credit1'],2);
        if ($needCredit2 > 0 ){
            $money['credit2'] = ($needCredit2 < $credit2) ? $needCredit2 : $credit2;
            $needCredit3= bcsub($needCredit2 ,$money['credit2'],2);
            if ($needCredit3 > 0){
                $money['credit4'] = ($needCredit3 < $credit4) ? $needCredit3 : $credit4;
            }
        }
    }
    if (empty($money)) {
        return ['code' => 0, 'data' => '没有数据'];
    } else {
        return ['code' => 1, 'data' => $money];
    }
}
/**
 * 金额计算 超级商城复投
 * @param int $credit1 电子币
 * @param int $credit2 奖励币
 * @param int $credit3 复投币
 * @param int $rulingprice 商品价格
 * @return array
 */
function FIGURE_FU($credit1 = 0, $credit2 = 0, $credit3 = 0, $rulingprice = 0)
{
    $stayCredit3 = RETAIN_TWO($rulingprice * 0.7); //待用
    if ($rulingprice == 610) {
        $stayCredit3 = 500;
    }
    if ($stayCredit3 > $credit3) {
        return ['code' => 0, 'data' => '余额不足'];
    }
    $totalMoney = $credit1 + $credit2 + $stayCredit3;
    $money['credit1'] = 0; // 电子币 默认电子币先用
    $money['credit2'] = 0; // 奖励币
    $money['credit3'] = 0; // 复投币
    if ($totalMoney < $rulingprice) {
        return ['code' => 0, 'data' => '余额不足，请充值电子币'];
    }
    $money['credit3'] = $stayCredit3;
    $stayCredit1 = $rulingprice - $money['credit3']; //除去奖励币还需支付
    if ($stayCredit1 > $credit1) {
        $money['credit1'] = RETAIN_TWO($credit1);
    } else {
        $money['credit1'] = RETAIN_TWO($stayCredit1);
    }
    if ($stayCredit1 > $credit1) {
        $money['credit2'] = RETAIN_TWO(($stayCredit1 - $money['credit1']));
    }
    if ($money['credit1'] <= 0) {
        $money['credit2'] = RETAIN_TWO(($rulingprice - $money['credit3']));
    }
    if (empty($money)) {
        return ['code' => 0, 'data' => '没有数据'];
    } else {
        return ['code' => 1, 'data' => $money];
    }
}
/**
 * 金额计算 直商城复投
 * @param int $credit1 电子币
 * @param int $credit2 奖励币
 * @param int $credit3 复投币
 * @param int $rulingprice 商品价格
 * @return array
 */
function FIGURE_MALL($credit1 = 0, $credit2 = 0, $credit6 = 0, $rulingprice = 0)
{
    $stayCredit6 = RETAIN_TWO($rulingprice * 0.7); //待用
    if ($stayCredit6 > $credit6) {
        return ['code' => 0, 'data' => '余额不足'];
    }
    $totalMoney = $credit1 + $credit2 + $stayCredit6;
    $money['credit1'] = 0; // 电子币 默认电子币先用
    $money['credit2'] = 0; // 奖励币
    $money['credit6'] = 0; // 复投币
    if ($totalMoney < $rulingprice) {
        return ['code' => 0, 'data' => '余额不足，请充值电子币'];
    }
    $money['credit6'] = $stayCredit6;
    $stayCredit1 = $rulingprice - $money['credit6']; //除去奖励币还需支付
    if ($stayCredit1 > $credit1) {
        $money['credit1'] = $credit1;
    } else {
        $money['credit1'] = $stayCredit1;
    }
    if ($stayCredit1 > $credit1) {
        $money['credit2'] = $stayCredit1 - $money['credit1'];
    }
    if ($money['credit1'] <= 0) {
        $money['credit2'] = $rulingprice - $money['credit6'];
    }
    if (empty($money)) {
        return ['code' => 0, 'data' => '没有数据'];
    } else {
        return ['code' => 1, 'data' => $money];
    }
}
/**
 * 金额计算 复消
 * @param int $credit1 电子币
 * @param int $credit2 奖励币
 * @param int $credit3 复投币
 * @param int $rulingprice 商品价格
 * @param int $freight 运费
 * @return array
 */
function VOTING($credit1 = 0,$credit2 = 0,$credit6 = 0,$rulingprice = 0,$freight = 0){
    $money['credit1']       = 0; //电子币 默认电子币先用
    $money['credit2']       = 0; //奖励币
    $money['credit6']       = 0;// 复投币
    $credit6s = $rulingprice;
    if($credit6 < $rulingprice){
        return ['code' => 0 , 'data' => '复投币不足'];
    }
    $money['credit6'] = $credit6s;
    //算运费

    if(($credit1 + $credit2) < $freight){
        return ['code' => 0 , 'data' => '余额不足'];
    }
    if($credit1<=$freight){
        $money['credit1'] = $credit1;
        $money['credit2'] = ($freight - $money['credit1']);
    } else {
        $money['credit1'] = $freight;
    }
    if (empty($money)) {
        return ['code' => 0 ,'data' => '没有数据'];
    } else {
        return ['code' => 1 ,'data' => $money];
    }
}



/**
 * 金额计算 复消
 * @param int $credit1 电子币
 * @param int $credit3 复消币
 * @param int $rulingprice 商品价格
 * @return array
 */
function FIGURE_FUXIAO($credit1 = 0, $credit2 = 0, $credit5 = 0, $rulingprice = 0)
{
    $redelivery_money = \App\Model\Poundage::where('id',1)->value('redelivery_money');
    $money['credit1'] = 0; //积分默认电子币先用
    $money['credit2'] = 0;//参与积分
    $money['credit5'] = 0;// 分享积分

    $redelivery = $redelivery_money * $rulingprice /100;
    $credit5 = ( $credit5 > $redelivery ) ? $redelivery : $credit5;
    $totalMoney = $credit1 + $credit2 + $credit5;

    if ($totalMoney < $rulingprice) {
        return ['code' => 0, 'data' => '余额不足'];
    }
    //使用复销币
    $money['credit5'] = $credit5;

    //使用电子币
    $needCredit1 = bcsub($rulingprice,$credit5,2); //除去复销币还需支付
    if ($needCredit1 > 0) {
        //电子币支付
        $money['credit1'] = ($needCredit1 < $credit1) ? $needCredit1 : $credit1;
        $needCredit2= bcsub($needCredit1,$money['credit1'],2);
        if ($needCredit2 > 0 ){
            $money['credit2'] = ($needCredit2 < $credit2) ? $needCredit2 : $credit2;
        }
    }

    if (empty($money)) {
        return ['code' => 0, 'data' => '没有数据'];
    } else {
        return ['code' => 1, 'data' => $money];
    }
}

/**
 * 计算 扣除平台费和复投费
 * @param array $Poundage 设置参数
 * @param int $recommend 奖励金额
 * * @param int $shopType 商城类型
 * @return array redelivery 复投费 money 最终得到的费用 platform_fee 平台费
 */
function CALCULATE($Poundage = array(), $recommend = 0 )
{
    //平台费
    $platform_fee = RETAIN_TWO($recommend * ($Poundage['platform_fee'] * 0.01));
    //复投费
    $redelivery = RETAIN_TWO($recommend * ($Poundage['redelivery'] * 0.01));
    //最终得到的费用
    $recommendMoney = $recommend - ($platform_fee + $redelivery);
//    $recommendMoney = $recommend - $platform_fee;
//    var_dump($recommendMoney);
//    var_dump($platform_fee);exit;
    return ['redelivery' => $redelivery, 'money' => $recommendMoney, 'platform_fee' => $platform_fee];
//    return [ 'money' => $recommendMoney, 'platform_fee' => $platform_fee];
}
/**
 * 计算 扣除平台费和复投费
 * @param array $Poundage 设置参数
 * @param int $recommend 奖励金额
 * * @param int $shopType 商城类型
 * @return array redelivery 复投费 money 最终得到的费用 platform_fee 平台费
 */
function CALCULATQ($Poundage = array(), $recommend = 0)
{
    //平台费
    $platform_fee = RETAIN_TWO($recommend * ($Poundage['platform_fee'] * 0.01));
    //复投费
    $redelivery = 0;
    //最终得到的费用
    $recommendMoney = $recommend - ($platform_fee + $redelivery);
//    $recommendMoney = $recommend - $platform_fee;
//    var_dump($recommendMoney);
//    var_dump($platform_fee);exit;
    return ['redelivery' => $redelivery, 'money' => $recommendMoney, 'platform_fee' => $platform_fee];
//    return [ 'money' => $recommendMoney, 'platform_fee' => $platform_fee];
}
/**
 * 计算 扣除平台费和复销费
 * @param array $Poundage 设置参数
 * @param int $recommend 奖励金额
 * * @param int $shopType 商城类型
 * @return array redelivery 复投费 money 最终得到的费用 platform_fee 平台费
 */
function CALCULATD($Poundage = array(), $recommend = 0 ,$shopType)
{
    //平台费
    $platform_fee = RETAIN_TWO($recommend * ($Poundage['platform_fee'] * 0.01));
    //复投费
    $redelivery = RETAIN_TWO($recommend * ($Poundage['subsidy_money'] * 0.01));
    //最终得到的费用
    $recommendMoney = $recommend - ($platform_fee + $redelivery);
//    $recommendMoney = $recommend - $platform_fee;
//    var_dump($recommendMoney);
//    var_dump($platform_fee);exit;
    return ['redelivery' => $redelivery, 'money' => $recommendMoney, 'platform_fee' => $platform_fee];
//    return [ 'money' => $recommendMoney, 'platform_fee' => $platform_fee];
}

//功能：签到时计算两个时间戳之间相差的日时分秒
//$begin_time 开始时间戳
//$end_time 结束时间戳
function timediff($begin_time, $end_time)
{
    if ($begin_time < $end_time) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    //计算天数
    $timediff = $endtime - $starttime;
    $days = intval($timediff / 86400);
    //计算小时数
    $remain = $timediff % 86400;
    $hours = intval($remain / 3600);
    //计算分钟数
    $remain = $remain % 3600;
    $mins = intval($remain / 60);
    //计算秒数
    $secs = $remain % 60;
    $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
    return $res;
}

/**
 *      *计算某个经纬度的周围某段距离的正方形的四个点
 *      *@param lng float 经度
 *      *@param lat float 纬度
 *      *@param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为0.5千米
 *      *@return array 正方形的四个点的经纬度坐标
 */
function RETURNSQUAREPOINT($lng, $lat, $distance = 5){
     $earthdata = 6371;//地球半径，平均半径为6371km
     $dlng = 2 * asin(sin($distance / (2 * $earthdata)) / cos(deg2rad($lat)));
     $dlng = rad2deg($dlng);
     $dlat = $distance / $earthdata;
     $dlat = rad2deg($dlat);
     $arr = array(
         'left_top' => array('lat' => $lat + $dlat, 'lng' => $lng - $dlng),
         'right_top' => array('lat' => $lat + $dlat, 'lng' => $lng + $dlng),
         'left_bottom' => array('lat' => $lat - $dlat, 'lng' => $lng - $dlng),
         'right_bottom' => array('lat' => $lat - $dlat, 'lng' => $lng + $dlng)
     );
     return $arr;
}
/**
 * 计算两点地理坐标之间的距离
 * @param  Decimal $longitude1 起点经度
 * @param  Decimal $latitude1  起点纬度
 * @param  Decimal $longitude2 终点经度
 * @param  Decimal $latitude2  终点纬度
 * @param  Int     $unit       单位 1:米 2:公里
 * @param  Int     $decimal    精度 保留小数位数
 * @return Decimal
 */
function GETDISTANCE($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){

    $EARTH_RADIUS = 6370.996; // 地球半径系数
    $PI = 3.1415926;

    $radLat1 = $latitude1 * $PI / 180.0;
    $radLat2 = $latitude2 * $PI / 180.0;

    $radLng1 = $longitude1 * $PI / 180.0;
    $radLng2 = $longitude2 * $PI /180.0;

    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;

    $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
    $distance = $distance * $EARTH_RADIUS * 1000;

    if($unit==2){
        $distance = $distance / 1000;
    }
    return round($distance, $decimal);
}


/**
 * 获取组合的结果
 * [@author](https://my.oschina.net/arthor) 18y
 * @anotherdate 2019-04-27T09:49:44+0800
 * [@param](https://my.oschina.net/u/2303379)       array                         $val 组合内容
 * [@return](https://my.oschina.net/u/556800)      [type]                        [description]
 */
function getCombinationToString($val,$num)
{
//     保存上一个的值
    static $res = array();
    if(empty($res))
    {
        $res = $val;
    } else {
        $list = array();
        // 临时数组保存结合的结果
        foreach ($res as $k => $v) {
            foreach ($val as $key => $value) {
                $list[] = $v.'_'.$value;
            }
        }
        foreach ($list as $k => $v) {
            $arr = explode('_',$v);
            if (count($arr) == $num){
                $res = array();
                return $list;
            } else {
                $res = $list;
            }
        }
    }
}

/**
 * 发送短信
 * @param $phone 手机号
 * @return array
 */
function sms($phone,$template)
{
    $rand = rand(100000, 999999);
    $url = 'https://open.ucpaas.com/ol/sms/sendsms';
    $body_json = array(
        'sid' => '77ea3e44d6dc277ac6a8b8d759145822',
        'token' => 'f001003e682eb15af50958a10bc0d873',
        'appid' => '899b2263e7804be9b901c58651ac0655',
        'templateid' => $template,
        'param' => $rand . ',10',
        'mobile' => $phone,
    );
    $header = array(
        'Accept:application/json',
        'Content-Type:application/json;charset=utf-8',
    );
    $data = httpRequest($url, 'post', json_encode($body_json), $header);
    $data = json_decode($data, true);
    var_dump($data);exit;
    if ($data['code'] == "000000") {
        return ['code' => 1, 'msg' => '短信获取成功'];
    }
    return ['code' => 0, 'msg' => $data];
}
/**
 * CURL请求
 * @param $url 请求url地址
 * @param $method 请求方法 get post
 * @param null $postfields post数据数组
 * @param array $headers 请求header信息
 * @param bool|false $debug  调试开启 默认false
 * @return mixed
 */
function httpRequest($url, $method="GET", $postfields = null, $headers = array(), $debug = false) {
    $method = strtoupper($method);
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间，如果设置为0，则无限等待 */
    curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    switch ($method) {
        case "POST":
            curl_setopt($ci, CURLOPT_POST, true);
            if (!empty($postfields)) {
                $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
                curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
            }
            break;
        default:
            curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
            break;
    }
    $ssl = preg_match('/^https:\/\//i',$url) ? TRUE : FALSE;
    curl_setopt($ci, CURLOPT_URL, $url);
    if($ssl){
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
    }
    //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
    curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量，这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
    /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
    $response = curl_exec($ci);
    $requestinfo = curl_getinfo($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
    if ($debug) {
        echo "=====post data======\r\n";
        var_dump($postfields);
        echo "=====info===== \r\n";
        print_r($requestinfo);
        echo "=====response=====\r\n";
        print_r($response);
    }
    curl_close($ci);
    return $response;
    //return array($http_code, $response,$requestinfo);
}


